package exa.unicen.edu.ringo.raccess.resources;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import exa.unicen.edu.ringo.raccess.model.manager.ConfigurationManager;
import exa.unicen.edu.ringo.rmodel.bean.Configuration;
import exa.unicen.edu.ringo.rmodel.bean.Node;
import exa.unicen.edu.ringo.rmodel.builder.ModelBuilder;

@Path("/nodes")
public class NodeResource {

	private ModelBuilder builder = ModelBuilder.instance;
	private ConfigurationManager manager = ConfigurationManager.instance;
	
	@POST
	@Consumes({"application/xml","text/xml"})
	@Produces({"application/xml","text/xml"})
	public String set(String xml) {
		System.out.println("Node [POST]: " + xml);
		
		Node node = (Node) builder.marshal(Node.class, xml);
		
		Configuration configuration = manager.get();
		configuration.nodes.put(node.id, node);
		
		return xml;
	}
	
	@GET
	@Path("/{id}")
	@Produces({"application/xml","text/xml"})
	public String get(@PathParam("id") String id) {
		System.out.println("Node [GET]: " + id);
		
		Configuration configuration = manager.get();
		Node node = configuration.nodes.get(id);

		return builder.unmarshal(Node.class, node);
	}
	

	
}
